package com.swadar.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.swadar.beans.dataobject.BrandDO;
import com.swadar.beans.dataobject.CategoryDO;
import com.swadar.beans.dataobject.UserDO;
import com.swadar.beans.dataobject.UserLoginDo;
import com.swadar.beans.valueobject.ProductVO;
import com.swadar.beans.valueobject.UserVO;
import com.swadar.dao.UserDAO;
import com.swadar.helper.ProductHomeHelper;


@Controller
public class ProductHomeController {
	
	@Autowired
	private ProductHomeHelper productHomeHelper;
	
	@Autowired
	private UserDAO userDAO;
	
	@RequestMapping("/productHome.do")
	public String showPagination(ModelMap modelMap, @RequestParam("brandId") Long brandId, @RequestParam(value="categoryId",required=false) Long categoryId){
		List<ProductVO> productList = productHomeHelper.getProductList(categoryId, brandId);
		List<CategoryDO> categoryList = productHomeHelper.getCategoryList();
		List<BrandDO> imageList = productHomeHelper.getBrandImage();
		modelMap.addAttribute("imageList", imageList);
		modelMap.addAttribute("productList", productList);
		modelMap.addAttribute("categoryList", categoryList);
		return "productHome";
	}
	
	@RequestMapping("/saveUser.do")
	public String saveUser(UserVO userVO){
		UserDO userDO = new UserDO();
		userDO.setName(userVO.getName());
		userDO.seteMail(userVO.geteMail());
		userDO.setGender(userVO.getGender());
		userDAO.addUser(userDO);
		Long userId = userDAO.getUserId();
		UserLoginDo userLoginDo = new UserLoginDo();
		userLoginDo.setUserId(userId);
		userLoginDo.setUserName(userVO.getName());
		userLoginDo.seteMail(userVO.geteMail());
		userLoginDo.setPassword(userVO.getPassword());
		userDAO.addUserLogin(userLoginDo);
		return "productHome";
	}
	
	@RequestMapping("Login.do")
	public boolean Validation(UserVO userVO){
		boolean valid = userDAO.Validation(userVO);
		return valid;
			
	}
	
}
